﻿using System.Collections.Generic;
using UnityEngine;

//服务状态
public enum ServerStatus
{
    Uphold,     //维护
    Fluent,     //流畅
    Busy,       //繁忙
    Hot         //火爆
}

public class ServerItem
{
    public int id;
    public string name;
    public bool isNew;
    public ServerStatus status;
}

public class ServerInfo
{
    public List<ServerItem> serverList = new List<ServerItem>();
}

public class ServerMgr
{
    private static ServerMgr instance = new ServerMgr();
    public static ServerMgr Instance { get => instance; }

    public ServerInfo serverInfo;
    string fileName = "ServerInfo";
    private ServerMgr()
    {
        this.Load();
    }

    private void Load()
    {
        this.serverInfo = JsonMgr.Instance.Read<ServerInfo>(fileName);
        if (this.serverInfo.serverList.Count == 0)
        {
            CreateTestData();
        }
    }

    private void Save()
    {
        JsonMgr.Instance.Save(fileName, this.serverInfo);
    }

    public void CreateTestData()
    {
        string[] names = new string[]
        {
           "天府之国",
           "东方明珠",
           "洞庭湖",
           "鼎立山河",
           "峰火关东",
           "飞龙在天",
           "福虎生威",
           "飞鸿踏雪",
           "峰烟再起",
           "黄鹤楼",
           "剑舞香江",
           "君临天下",
           "莫问今朝",
           "上善若水",
           "天下无双",
           "五台山",
           "乐动山河",
           "烟花三月",
           "再战今朝",
           "逐鹿中原",
           "紫禁之巅",
           "盛世长安",
        };

        for (int i = 0; i < 105; i++)
        {
            ServerItem item = new ServerItem();
            item.id = i;
            item.name = names[Random.Range(0, names.Length)];
            item.isNew = Random.Range(0, 100) > 80 ? true : false;
            item.status = (ServerStatus)Random.Range((int)ServerStatus.Uphold, (int)ServerStatus.Hot + 1);
            this.serverInfo.serverList.Add(item);
        }
        JsonMgr.Instance.Save(fileName, this.serverInfo);
    }

    public int GetMaxPage(int size)
    {
        return (this.serverInfo.serverList.Count / size) + (this.serverInfo.serverList.Count % size == 0 ? 0 : 1);
    }

    public int GetCount()
    {
        return this.serverInfo.serverList.Count;
    }

    public ServerItem GetServer(int id)
    {
        for (int i = 0; i < serverInfo.serverList.Count; i++)
        {
            if (serverInfo.serverList[i].id == id)
            {
                return serverInfo.serverList[i];
            }
        }
        return null;
    }

    public List<ServerItem> GetServerList(int startId, int endId)
    {
        List<ServerItem> result = new List<ServerItem>();
        for (int i = 0; i < serverInfo.serverList.Count; i++)
        {
            ServerItem item = serverInfo.serverList[i];
            if (item.id >= startId && item.id <= endId)
            {
                result.Add(serverInfo.serverList[i]);
            }
        }
        return result;
    }
}
